// Copyright 2009 Pigasus.
// GNU GPL v2.
// Author: John Zhang.

#include "MiniCppUnit-2.5/MiniCppUnit.hxx"

#include <vector>
#include <string>

#include "../src/pigasus-types.h"
#include "../src/loadcsv.h"

class LoadCSVTest : public TestFixture<LoadCSVTest> {
 private:
  void GetValuesFromMap(Map &row, string *host, string *password,
                        string *user) {
    *host = *((string*)(row["host"]));
    *password = *((string*)(row["password"]));
    *user = *((string*)(row["user"]));
  }

 public:
  TEST_FIXTURE(LoadCSVTest) {
    TEST_CASE(TestBasicCSVRead);
    TEST_CASE(TestElementWithNewLine);
  }

  void TestBasicCSVRead() {
    List contents = LoadCSV("samples/csv_test_1.csv");
    string host;
    string user;
    string password;

    GetValuesFromMap(*(Map*)(contents[0]), &host, &password, &user);
    ASSERT_EQUALS(user, "john");
    ASSERT_EQUALS(host, "cepsr");
    ASSERT_EQUALS(password, "zhang");

    GetValuesFromMap(*(Map*)(contents[1]), &host, &password, &user);
    ASSERT_EQUALS(user, "jordan");
    ASSERT_EQUALS(host, "\"csv,bob\"");
    ASSERT_EQUALS(password, "bobo");
  }

  void TestElementWithNewLine() {
    List contents = LoadCSV("samples/csv_test_2.csv");
    string host;
    string user;
    string password;

    GetValuesFromMap(*(Map*)(contents[0]), &host, &password, &user);
    ASSERT_EQUALS(user, "john");
    ASSERT_EQUALS(host, "\"csv,\nbob\"");
    ASSERT_EQUALS(password, "");
  }
};

REGISTER_FIXTURE(LoadCSVTest);

